#include <buola/chart/cxaxis.h>

#include <buola/image/cgc.h>
#include <buola/chart/cyaxis.h>
#include <buola/chart/cxychart.h>

namespace buola { namespace chart {

CXAxis::CXAxis()
{
}

CXAxis::~CXAxis()
{
}

bool CXAxis::CanZoom()
{
    return true;
}

void CXAxis::ReduceRect(CXYChartContext &pC)
{
    pC.mRect.b-=40;
    if(pC.mChart->Style(CXYChart::BOX))
    {
        pC.mRect.t+=5;
    }
}

void CXAxis::CalcLimits(CXYChartContext &pC)
{
    mRect=pC.mRect;
    
    if(mRect.w()<0) return;
    
    mMapping->SetDestLimits(mRect.l+pC.mExtraSpace.l,
                           mRect.r-pC.mExtraSpace.r);

    if(mAutoScale)
    {
        mMapping->AutoScale(pC.mMinX,pC.mMinPosX,pC.mMaxX,false,50.0);
    }
}

void CXAxis::Draw(CXYChartContext &pC)
{
    bool lThin=pC.mChart->Style(CXYChart::THIN);
    
    if(lThin)
    {
        pC.mGC->SetLineWidth(0.8);
    }
    else
    {
        pC.mGC->SetLineWidth(1.5);
    }

    if(mLabelNames.size())
    {
        pC.mGC->SetSource(img::CColor(0,0,0));

        for(int i=0;i<mLabelNames.size();i++)
        {
            CPoint_d lPoint(Map(0.5+i),mRect.b);
  
            ///\todo matrices don't work yet anyway
//            pC.mGC->PushState();

            pC.mGC->Translate(lPoint);
            pC.mGC->Rotate(-M_PI/3);
            pC.mGC->DrawText(CPoint_d(-5,0),mLabelNames[i],img::ETextPos::RIGHT|img::ETextPos::VCENTER);

//            pC.mGC->PopState();
        }
    }
    else
    {
        img::CColor lLinesColor(0,0,0);
        lLinesColor.a=0.2;
    
        bool lTopTicks=pC.mChart->Style(CXYChart::BOX);

        std::vector<double> lTicks;
        mMapping->GetTicks(lTicks,50.0);

        for(int i=0;i<lTicks.size();i++)
        {
            if(std::abs(lTicks[i])<1e-10)
                lTicks[i]=0;

            double lXPos=Map(lTicks[i]);
            if(lThin)
                lXPos=round(lXPos-0.5)+0.5;
            pC.mGC->SetSource(img::CColor(0,0,0));
            pC.mGC->Path().MoveTo(CPoint_d(lXPos,mRect.b-7));
            pC.mGC->Path().LineTo(CPoint_d(lXPos,mRect.b));
            pC.mGC->StrokePath();
        
            if(lTopTicks)
            {
                pC.mGC->Path().MoveTo(CPoint_d(lXPos,mRect.t));
                pC.mGC->Path().LineTo(CPoint_d(lXPos,mRect.t+7));
                pC.mGC->StrokePath();
            }
        
            pC.mGC->DrawText(CPoint_d(lXPos,mRect.b+4),to_wstring(lTicks[i]),img::ETextPos::HCENTER);
        
            pC.mGC->SetLineStyle(img::ELineStyle::ONOFF);
            pC.mGC->SetSource(lLinesColor);
            pC.mGC->Path().MoveTo(CPoint_d(lXPos,lTopTicks?(mRect.t+7):mRect.t));
            pC.mGC->Path().LineTo(CPoint_d(lXPos,mRect.b-7));
            pC.mGC->StrokePath();
            pC.mGC->SetLineStyle(img::ELineStyle::SOLID);
        }

        if(mTitle.length())
        {
            pC.mGC->SetSource(img::CColor(0,0,0));
            pC.mGC->DrawText(CPoint_d((mRect.l+mRect.r)/2.0,mRect.b+20),mTitle,img::ETextPos::CENTER);
        }
        if(mMark.mEnabled)
        {
            double lXPos=Map(mMark.mPos);
            if(lThin)
                lXPos=round(lXPos-0.5)+0.5;
            pC.mGC->SetSource(img::CColor(0,0,0,0.6));
            pC.mGC->Path().MoveTo(CPoint_d(lXPos,mRect.t));
            pC.mGC->Path().LineTo(CPoint_d(lXPos,mRect.b));
            pC.mGC->StrokePath();
        }
    }
}

/*namespace buola*/ } /*namespace buola*/ }
